﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.SCM.Item;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// ItemConvertRatioSaveSV partial 
    /// </summary>	
	public partial class ItemConvertRatioSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new ItemConvertRatioSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class ItemConvertRatioSaveSVImpementStrategy : BaseStrategy
	{
		public ItemConvertRatioSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			ItemConvertRatioSaveSV bpObj = (ItemConvertRatioSaveSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();
                foreach (var dto in bpObj.Data)
                {
                    if (dto.ItemMaster == null) throw new Exception("ItemMaster必填");
                    if (dto.FromUOM == null) throw new Exception("FromUOM必填");
                    if (dto.ToUOM == null) throw new Exception("ToUOM必填");
                    if (dto.Ratio == 0) throw new Exception("Ratio不能为0");
                    //string path = $"ItemMaster.Code='{dto.ItemMaster.Code}' and FromUOM.Code='{dto.FromUOM.Code}' and ToUOM.Code='{dto.ToUOM.Code}'";
                    //ItemConvertRatioOverClass entity = ItemConvertRatioOverClass.Finder.Find(path);
                    ItemConvertRatioOverClass entity = ItemConvertRatioBase.GetEntity(dto);

                    if (entity == null)
                    {
                        //创建项目档案
                        entity = ItemConvertRatioBase.CreateDoc(dto);
                    }
                    else
                    {
                        //修改项目档案
                        entity = ItemConvertRatioBase.UpdateDoc(dto,entity);
                    }

                    //处理返回值
                    retlist.Add(new CopyOfCommonArchiveDataDTOData
                    {
                        ID = entity.ID,
                        Code = entity.ItemMaster.Code,
                        Name = entity.ItemMaster.Name,
                    });
                }

                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}